package com.testin.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class DBUtils {
	  static {//静态初始化语句块
			try {
				Class.forName(ConfigurationUtils.DB_DRIVER);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		public static Connection getConnection() throws Exception {
			return getConnection(true);
		}
		
		public static Connection getConnection(boolean autoCommit) throws Exception {
			Connection conn = DriverManager.getConnection(ConfigurationUtils.DB_URL, ConfigurationUtils.DB_USERNAME, ConfigurationUtils.DB_PASSWORD);
			conn.setAutoCommit(autoCommit);
			return conn;
		}
	
	public static void close(ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void close(Statement stmt) {
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void close(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void close(Statement stmt, Connection conn) {
		close(stmt);
		close(conn);
	}
	
	public static void close(ResultSet rs, Statement stmt, Connection conn) {
		close(rs);
		close(stmt, conn);
	}
}
